/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

/**
 *
 * @author David
 */
import java.util.*;
import java.io.*;
import AirLine.*;
import Database.DBObject;
import java.sql.*;
import java.math.*;

public class RandomDataCreatorBjornFredrik {
/*
    private Set<Airport> airportList;
    private Set<Flight> flightList;
    private ArrayList<Airplane> planeList;
    private ArrayList<Airport> forFlights;
    private int nrFlights,  nrRows,  nrColumns,  nrPlanes;
    String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    //private String[] alpha;
 * */
    public static void main(String[] args) {
        new RandomDataCreatorBjornFredrik();

    }

    public RandomDataCreatorBjornFredrik() {
     
        //System.out.println(Math.random());
        RandomDataCreator.initialize();

        //addAirline();
       
       if(true)
       {
           if(true)
           {
               // addAirports();
                //addAirline();
           }

        addPlanes();
        addSeats();
        addFlights();
       }
       else if(true)
           removeStuff();
    }

    private void removeStuff()
    {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("DELETE FROM SEAT");
            stm.execute();
            mm.commit();
            /*
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("DELETE FROM AIRLINEINFO");
            stm.execute();
            mm.commit();
            */
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("DELETE FROM FLIGHT");
            stm.execute();
            mm.commit();
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("DELETE FROM AIRPLANE");
            stm.execute();
            mm.commit();
//            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("DELETE FROM AIRPORT");
//            stm.execute();
//            mm.commit();
            
        } catch (SQLException e) {
            try {
                System.out.println("fel i fÃƒÂ¶rsta..." + e.getLocalizedMessage());
                mm.rollBack();
                
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }
    
    private void addAirline()
    {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("INSERT INTO AIRLINEINFO (NAME, CODE, OWNDISCOUNT) values(?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            
                try {
                    stm.setString(1, "Flygande frasse");
                    stm.setString(2, DBObject.ORACLE_033);
                    stm.setFloat(3, 15);
                    stm.execute();
                    mm.commit();
                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i fÃ¶rsta..." + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }
                }
            
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i fÃ¶rsta..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }
    
    private void addAirports() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("INSERT INTO AIRPORT (NAME, CODE) values(?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airport a : RandomDataCreator.getAirports()) {
                try {
                    stm.setString(1, a.getAirportName());
                    stm.setString(2, a.getAirportCode());
                    stm.execute();
                    mm.commit();
                    
                } catch (SQLException e) {
                    try {
                        System.out.println("fel i fÃƒÂ¶rsta..." + e.getLocalizedMessage());
                        mm.rollBack();
                        
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }
                }
            }
        } catch (SQLException e) {
            try {
                System.out.println("fel i fÃƒÂ¶rsta..." + e.getLocalizedMessage());
                mm.rollBack();
                
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addPlanes()
    {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("INSERT INTO AIRPLANE (ID, TYPE) values(?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airplane p : RandomDataCreator.getAirplanes()) {
                                   try {
                        //System.out.println("lÃ¤gger in plane");
                        stm.setString(1, p.getId());
                        stm.setString(2, p.getType());
                        stm.execute();
                        mm.commit();
                    } catch (SQLException e) {
                        try {
                            System.out.println("fel i addplanes, CHECK..." + e.getLocalizedMessage());
                            mm.rollBack();
                            
                        } catch (Exception se) {
                            System.out.println("feeel");
                        }
                    }
                                     }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i fÃƒÂ¶rsta..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
        
    }
            
    
    private void addSeats() {
        DBObject mm = new DBObject();
        PreparedStatement stm;
        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("INSERT INTO SEAT (PLANEID, ROWNUMBER, POSITION, TYPE) values(?,?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Airplane p : RandomDataCreator.getAirplanes()) {
                for (Seat s : p.getSeats()) {
                    try {
                        
                      // System.out.println("planet vi fÃƒÂ¶rsÃƒÂ¶ker med: "+p.getId());
                        stm.setString(1, p.getId());
                        stm.setInt(2, s.getRow());
                        stm.setString(3, ""+s.getColumn());
                        stm.setInt(4, s.getType());
                        stm.execute();
                        mm.commit();
                    } catch (SQLException e) {
                        try {
                            mm.rollBack();
                            System.out.println("fel i addseats..." + e.getLocalizedMessage());
                        } catch (Exception se) {
                            System.out.println("feeel");
                        }
                    }
                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i fÃƒÂ¶rsta..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }
        }
    }

    private void addFlights() {
        DBObject mm = new DBObject();
        PreparedStatement stm;

        try {
            mm.connect();
            stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("INSERT INTO FLIGHT (IDNBR, DEPARTURETIME, ARRIVALTIME, DEPARTURE, DESTINATION, PLANE, FARE) values(?,?,?,?,?,?,?)");
            //stm = mm.getConnection(DBObject.ORACLE_033).prepareStatement("UPDATE AIRPORT SET CODE = ?");
            for (Flight f : RandomDataCreator.getFlights()) {
                try {
                    stm.setString(1, f.getCode());
                    
                    stm.setTimestamp(2, f.getDepartureTime());
                   
                    stm.setTimestamp(3, f.getArrivalTime());
                    
                    stm.setString(4, f.getDepartureAirport().getAirportCode());
                    stm.setString(5, f.getDestinationAirport().getAirportCode());
                    //System.out.println(f.getPlane().getId());
                    
                    stm.setString(6, f.getPlaneId());
                    //System.out.println("detta Ã¤r efter");
                    stm.setInt(7, f.getPrice());
                    // stm.setString(2, a.getAirportCode());
                    stm.execute();
                    
                    mm.commit();
                } catch (SQLException e) {
                    try {
                        mm.rollBack();
                        System.out.println("fel i addflights" + e.getLocalizedMessage());
                    } catch (Exception se) {
                        System.out.println("feeel");
                    }

                }
            }
        } catch (SQLException e) {
            try {
                mm.rollBack();
                System.out.println("fel i fÃƒÂ¶rsta..." + e.getLocalizedMessage());
            } catch (Exception se) {
                System.out.println("feeel");
            }

        }
    }
/*
    public ArrayList<Airplane> getPlanes() {
        Set<Seat> seats;
        String planeId;
        Airplane p;

        for (int i = 0; i < nrPlanes; i++) {
            seats = new HashSet<Seat>();
            planeId = getRandomLetters(8);
            for (int j = 0; j < nrRows; j++) {
                for (int k = 0; k <
                        nrColumns; k++) {
                    seats.add(new Seat(false, (int) (Math.random() * 3), planeId, j, k));
                }

            }
            p=new Airplane(planeId, seats, getRandomLetters(15));
            planeList.add(p);
        }
        return planeList;
    }

    public Set<Airport> getAirports() {
        try {
            //System.out.println(new File("woot").getAbsolutePath());
            Scanner sc = new Scanner(new File("airportlist.txt"));
            sc.useDelimiter("--");
            String s,
                    s2, s3;
            String name,
                    code;
            int begin, end;
            while (sc.hasNext()) {
                s = sc.next();

                //System.out.println(s.contains("/n"));
                begin =
                        s.length() - 5;
                end =
                        s.length() - 2;
                //System.out.println(s);
                name =
                        (s.subSequence(0, begin)).toString();
                code =
                        (s.subSequence(begin, end)).toString();
                airportList.add(new Airport(name, code));
                forFlights.add(new Airport(name, code));
            //System.out.println(s.subSequence(0, begin).toString());
            //System.out.println(s.subSequence(begin, end).toString());
            //System.out.println(sc.next()+"***");
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return airportList;
    }

    public long getRdmTimestamp(long start, long end) {
        long time = start + ((long) Math.random() * (end - start));
        return time;
    }

    public Set<Flight> getFlights() {
        Flight f;
        String flightId ="";
        Airport from, to;
        int price;
        Timestamp dep, arr;
        Airplane plane;
        // String[] alpha = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

        for (int i = 0; i <
                nrFlights; i++) {
            flightId = getRandomLetters(6);
            //alpha[(int) (Math.random() * 26)] + alpha[(int) (Math.random() * 26)] + (int) (Math.random() * 10) + (int) (Math.random() * 10) + (int) (Math.random() * 10) + (int) (Math.random() * 10);

            from =
                    forFlights.get((int) (Math.random() * forFlights.size()));
            to =
                    forFlights.get((int) (Math.random() * forFlights.size()));
            price =
                    (int) (Math.random() * 10000);

            plane=planeList.get((int) (Math.random() * planeList.size()));
            
            Timestamp[] ts=RandomDataCreator.getTimes();
            dep = ts[0];
            arr=ts[1];

            f =
                    new Flight(flightId, from, to, price, dep, arr, null, "airlineX", plane);
            flightList.add(f);
       
        }

        return flightList;
    }
 * */
}
